package com.buanalintas.sandals.pos.reportsaleinventory;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Bynga;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Formatter;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Template;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.bynga.formatter.NumberFormatter;
import com.buanalintas.bynga.generator.PageList;
import com.buanalintas.sandals.pos.entities.PosTransactionSaleShop;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

public class ReportSaleInventory extends Page {

    private Field saleShopId;
    private Field saleShopUnitId;
    private Field saleShopUserId;
    private Field saleShopNumber;
    private Field saleShopSubtotal;
    private Field saleShopDiscountPercent;
    private Field saleShopDiscountNominal;
    private Field saleShopTotal;
    private Field saleShopDate;
    private Field saleShopTimestamp;
    private Formatter format;
    private static String MESSAGE_DAILY = "<!--MESSAGE-DAILY-->";
    private static String MESSAGE_WEEKLY = "<!--MESSAGE-WEEKLY-->";
    private static String MESSAGE_MONTHLY_YEAR = "<!--MESSAGE-MONTHLY-YEAR-->";
    private static String MESSAGE_MONTHLY_MONTH = "<!--MESSAGE-MONTHLY-MONTH-->";
    private static String MESSAGE_YEARLY = "<!--MESSAGE-YEARLY-->";
    private static String MESSAGE_CUSTOM = "<!--MESSAGE-CUSTOM-->";
    private static String SELECT_MODE = "<!--SELECT-MODE-->";
    private static String SELECT_MODE_ONCHANGE = "<!--SELECT-MODE-ONCHANGE-->";
    private static String SELECT_VALUES = "<!--SELECT-VALUES-->";
    private static String SELECT_FILTER = "<!--SELECT-FILTER-->";
    private static String SELECT_FILTER_VALUES = "<!--SELECT-FILTER-VALUES-->";
    private static String MODE_DAILY = "MODE-DAILY";
    private static String DAILY_SELECT_DATE = "<!--DAILY-SELECT-DATE-->";
    private static String DAILY_DATE_VALUE = "<!--DAILY-DATE-VALUE-->";
    private static String MODE_WEEKLY = "MODE-WEEKLY";
    private static String WEEKLY_START_DATE_VALUE = "<!--WEEKLY-START-DATE-VALUE-->";
    private static String WEEKLY_SELECT_START_DATE = "<!--WEEKLY-SELECT-START-DATE-->";
    private static String MODE_MONTHLY = "MODE-MONTHLY";
    private static String MONTHLY_SELECT_YEAR = "<!--MONTHLY-SELECT-YEAR-->";
    private static String MONTHLY_SELECT_YEAR_VALUES = "<!--MONTHLY-SELECT-YEAR-VALUES-->";
    private static String MONTHLY_SELECT_MONTH = "<!--MONTHLY-SELECT-MONTH-->";
    private static String MONTHLY_SELECT_MONTH_VALUES = "<!--MONTHLY-SELECT-MONTH-VALUES-->";
    private static String MODE_YEARLY = "MODE-YEARLY";
    private static String YEARLY_SELECT_YEAR = "<!--YEARLY-SELECT-YEAR-->";
    private static String YEARLY_SELECT_YEAR_VALUES = "<!--YEARLY-SELECT-YEAR-VALUES-->";
    private static String MODE_CUSTOM = "MODE-CUSTOM";
    private static String CUSTOM_SELECT_START_DATE = "<!--CUSTOM-SELECT-START-DATE-->";
    private static String CUSTOM_START_DATE_VALUE = "<!--CUSTOM-START-DATE-VALUE-->";
    private static String CUSTOM_SELECT_END_DATE = "<!--CUSTOM-SELECT-END-DATE-->";
    private static String CUSTOM_END_DATE_VALUE = "<!--CUSTOM-END-DATE-VALUE-->";
    private static String BUTTON_SEARCH = "<!--BUTTON-SEARCH-->";
    private static String LIST_DATA = "<!--LIST-DATA-->";
    private static String RECORD = "<!--RECORD-->";
    private static String WARNING = "<!--WARNING-->";
    private static String TOTAL = "TOTAL";
    private static String TOTAL_CAPTION = "<!--TOTAL-CAPTION-->";
    private static String TOTAL_TEXT = "<!--TOTAL-TEXT-->";
    private static String RECAP_EXPORT_LINK = "<!--RECAP-EXPORT-LINK-->";
    private static String RECAP_EXPORT = "RECAP-EXPORT";
    private Action actionShow = new Action("show", "Show report sale shop");
    private Action actionExport = new Action("export", "Export report sale shop");

    public ReportSaleInventory() {

        super();
        setTheClass(PosTransactionSaleShop.class);

        saleShopId = new Field();
        saleShopId.setName("saleShopId");
        saleShopId.setCaption("id");
        saleShopId.setListProperties("");
        saleShopId.setInputProperties("");
        getFields().add(saleShopId);

        saleShopUnitId = new Field();
        saleShopUnitId.setName("saleShopUnitId");
        saleShopUnitId.setCaption("unit id");
        saleShopUnitId.setListProperties("");
        saleShopUnitId.setInputProperties("");
        getFields().add(saleShopUnitId);

        saleShopUserId = new Field();
        saleShopUserId.setName("saleShopUserId");
        saleShopUserId.setCaption("user id");
        saleShopUserId.setListProperties("");
        saleShopUserId.setInputProperties("");
        getFields().add(saleShopUserId);

        saleShopNumber = new Field();
        saleShopNumber.setName("saleShopNumber");
        saleShopNumber.setCaption("number");
        saleShopNumber.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        saleShopNumber.setInputProperties(" size=\"20\" readonly");
        saleShopNumber.setRequired(true);
        getFields().add(saleShopNumber);

        saleShopSubtotal = new Field();
        saleShopSubtotal.setName("saleShopSubtotal");
        saleShopSubtotal.setCaption("subtotal");
        saleShopSubtotal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        saleShopSubtotal.setInputProperties(" size=\"8\"");
        saleShopSubtotal.setRequired(true);
        saleShopSubtotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(saleShopSubtotal);

        saleShopDiscountPercent = new Field();
        saleShopDiscountPercent.setName("saleShopDiscountPercent");
        saleShopDiscountPercent.setCaption("discount percent");
        saleShopDiscountPercent.setListProperties(" align=\"right\" width=\"15%\" nowrap");
        saleShopDiscountPercent.setInputProperties(" size=\"8\"");
        getFields().add(saleShopDiscountPercent);

        saleShopDiscountNominal = new Field();
        saleShopDiscountNominal.setName("saleShopDiscountNominal");
        saleShopDiscountNominal.setCaption("discount nominal");
        saleShopDiscountNominal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        saleShopDiscountNominal.setInputProperties(" size=\"8\"");
        saleShopDiscountNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(saleShopDiscountNominal);

        saleShopTotal = new Field();
        saleShopTotal.setName("saleShopTotal");
        saleShopTotal.setCaption("total");
        saleShopTotal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        saleShopTotal.setInputProperties(" size=\"8\"");
        saleShopTotal.setRequired(true);
        saleShopTotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(saleShopTotal);

        saleShopDate = new Field();
        saleShopDate.setName("saleShopDate");
        saleShopDate.setCaption("date");
        saleShopDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        saleShopDate.setInputProperties(" size=\"8\" readonly");
        saleShopDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(saleShopDate);

        saleShopTimestamp = new Field();
        saleShopTimestamp.setName("saleShopTimestamp");
        saleShopTimestamp.setCaption("timestamp");
        saleShopTimestamp.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        saleShopTimestamp.setInputProperties(" size=\"8\" readonly");
        saleShopTimestamp.setDefaultValue(Utility.getTimestamp());
        getFields().add(saleShopTimestamp);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(saleShopDate);
        getFieldsAdd().add(saleShopNumber);
        getFieldsAdd().add(saleShopDiscountPercent);
        getFieldsAdd().add(saleShopDiscountNominal);

        getFieldsEdit().clear();
        getFieldsEdit().add(saleShopNumber);
        getFieldsEdit().add(saleShopDate);
        getFieldsEdit().add(saleShopDiscountPercent);
        getFieldsEdit().add(saleShopDiscountNominal);

        getFieldsList().clear();
        getFieldsList().add(saleShopNumber);
        getFieldsList().add(saleShopTimestamp);
        getFieldsList().add(saleShopSubtotal);
        getFieldsList().add(saleShopDiscountPercent);
        getFieldsList().add(saleShopDiscountNominal);
        getFieldsList().add(saleShopTotal);

        getFieldsView().clear();
        getFieldsView().add(saleShopNumber);
        getFieldsView().add(saleShopSubtotal);
        getFieldsView().add(saleShopDiscountPercent);
        getFieldsView().add(saleShopDiscountNominal);
        getFieldsView().add(saleShopTotal);
        getFieldsView().add(saleShopTimestamp);

        getActions().add(actionShow);
        getActions().add(actionExport);
    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        saleShopDate.setFormater(DateFormatter.getInstanceDate(this));
        format = DateFormatter.getInstanceDateTime(this);
    }

    @Override
    public boolean isSearchVisible() {
        return false;
    }

    @Override
    public boolean isTitleVisible() {
        return false;
    }

    @Override
    public StringBuffer process() {

        if (getAction().equals(actionExport)) {
            try {
                updateExportParams();
                if ("2".equals(getCurrentUser().getUserExport())) {
                    getResponse().setHeader("Content-Type", "application/vnd.ms-excel ");
                } else {
                    getResponse().setHeader("Content-Type", "application/pdf");
                }
                JRDataSource datasource = null;
//                String filter = Utility.getParameterEmpty(getRequest(), "filter");
                String mode = getRequest().getParameter("mode");

                if ("1".equals(mode)) {
                    String filterDate = Utility.getParameterEmpty(getRequest(), "recapDate");
                    getExportParams().put("title", Dictionary.getValue(this, "Report Sale Shop") + " #" + Dictionary.getValue(this, "Daily"));
                    if (!"".equals(filterDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "Date") + " " + Utility.getPostParameterEmpty(this, "recapDate"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);

                        getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));

                        getController().setPaginationFilter("&mode=" + mode + "&recapDate=" + filterDate);
                    }

                } else if ("2".equals(mode)) {
                    String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                    if (!"".equals(filterStartDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate"));
                        getExportParams().put("title", Dictionary.getValue(this, "Report Sale Shop") + " #" + Dictionary.getValue(this, "Weekly"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) dateFormatter.parse(filterStartDate));
                        String start = dateFormatter.format(cal.getTime());
                        cal.add(Calendar.DATE, 7);
                        String end = dateFormatter.format(cal.getTime());
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        getController().setPaginationFilter("&mode=" + mode + "&startDate=" + filterStartDate);
                    }

                } else if ("3".equals(mode)) {
                    String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                    String filterMonth = Utility.getParameterEmpty(getRequest(), "recapMonth");
                    if (!"".equals(filterYear) && !"".equals(filterMonth)) {
                        getExportParams().put("filter", Utility.getPostParameterEmpty(this, "recapYear") + " - " + Dictionary.getValue(this, Utility.getLongMonthName(Integer.parseInt(Utility.getPostParameterEmpty(this, "recapMonth")))));
                        getExportParams().put("title", Dictionary.getValue(this, "Report Sale Shop") + " #" + Dictionary.getValue(this, "Monthly"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", filterMonth);
                        Utility.replaceAll(date, "dd", "01");
                        String start = date.toString();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) dateFormatter.parse(start));
                        int max = cal.getActualMaximum(Calendar.DATE);
                        date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", filterMonth);
                        Utility.replaceAll(date, "dd", "" + max);
                        String end = date.toString();
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        getController().setPaginationFilter("&mode=" + mode + "&recapYear=" + filterYear + "&filterMonth=" + filterMonth);
                    }

                } else if ("4".equals(mode)) {
                    String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                    if (!"".equals(filterYear)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "Year") + " " + Utility.getPostParameterEmpty(this, "recapYear"));
                        getExportParams().put("title", Dictionary.getValue(this, "Report Sale Shop") + " #" + Dictionary.getValue(this, "Yearly"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", "01");
                        Utility.replaceAll(date, "dd", "01");
                        String start = date.toString();
                        date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", "12");
                        Utility.replaceAll(date, "dd", "31");
                        String end = date.toString();
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        getController().setPaginationFilter("&mode=" + mode + "&recapYear=" + filterYear);
                    }

                } else if ("5".equals(mode)) {
                    String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                    String filterEndDate = Utility.getParameterEmpty(getRequest(), "endDate");
                    getExportParams().put("title", Dictionary.getValue(this, "Report Sale Shop") + " #" + Dictionary.getValue(this, "Custom"));

                    if (!"".equals(filterStartDate) && "".equals(filterEndDate)) {
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        getController().setWhere("t.saleShopTimestamp >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));


                    } else if ("".equals(filterStartDate) && !"".equals(filterEndDate)) {
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        getController().setWhere("t.saleShopTimestamp <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));


                    } else if (!"".equals(filterStartDate) && !"".equals(filterEndDate)) {
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));

                    }
                }
                datasource = new JRBeanArrayDataSource(getController().getData(true).toArray());
                JasperPrint jasperPrint = JasperFillManager.fillReport(getClass().getResourceAsStream("ReportSaleInventory.jasper"), getExportParams(), datasource);
                if ("2".equals(getCurrentUser().getUserExport())) {
                    JRXlsExporter excelExporter = new JRXlsExporter();
                    excelExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
                    excelExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, getResponse().getOutputStream());
                    excelExporter.exportReport();
                } else {
                    JasperExportManager.exportReportToPdfStream(jasperPrint, getResponse().getOutputStream());
                }
                return null;
            } catch (Exception e) {
                Logger.getInstance().log(e);
            }
            return new StringBuffer();

        } else {
            StringBuffer content = Utility.load(Bynga.BYNGA_PACKAGE_THEME + "content.html");
            Template template = new Template();
            template.loadTemplate(getClass().getResourceAsStream("/com/buanalintas/sandals/pos/reportsaleinventory/recap.html"));

            template.addValue(MESSAGE_DAILY, Dictionary.getValue(this, "Please enter date"));
            template.addValue(MESSAGE_WEEKLY, Dictionary.getValue(this, "Please enter start date"));
            template.addValue(MESSAGE_MONTHLY_YEAR, Dictionary.getValue(this, "Please select year"));
            template.addValue(MESSAGE_MONTHLY_MONTH, Dictionary.getValue(this, "Please select month"));
            template.addValue(MESSAGE_YEARLY, Dictionary.getValue(this, "Please select year"));
            template.addValue(MESSAGE_CUSTOM, Dictionary.getValue(this, "Please enter start date or end date or both"));

//            template.addValue(SELECT_FILTER, Dictionary.getValue(this, "Select customers"));
//            String selectFilter = "<option value=\"*\">" + Dictionary.getValue(this, "All Transaction Shop") + "</option>";
//            String filter = Utility.getParameterEmpty(getRequest(), "filter");
//            template.addValue(SELECT_FILTER_VALUES, selectFilter);

            template.addValue(SELECT_MODE, Dictionary.getValue(this, "Select mode"));
            template.addValue(SELECT_MODE_ONCHANGE, Dictionary.getValue(this, "'?m=" + getMenu().getMenuName() + "&a=" + getMenu().getMenuAction() + "&mode='+this.value"));

            String selectMode = "";
            String mode = Utility.getParameterEmpty(getRequest(), "mode");
            if ("".equals(mode)) {
                mode = "1";
            }
            selectMode += "<option value=\"1\"" + ("1".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Daily") + "</option>";
            selectMode += "<option value=\"2\"" + ("2".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Weekly") + "</option>";
            selectMode += "<option value=\"3\"" + ("3".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Monthly") + "</option>";
            selectMode += "<option value=\"4\"" + ("4".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Yearly") + "</option>";
            selectMode += "<option value=\"5\"" + ("5".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Custom") + "</option>";
            template.addValue(SELECT_VALUES, selectMode);
            boolean complete = false;

            if ("1".equals(mode)) {
                StringBuffer parameter = template.getEmptyElement(MODE_DAILY);
                Utility.replaceAll(parameter, DAILY_SELECT_DATE, Dictionary.getValue(this, "Date"));
                Utility.replaceAll(parameter, DAILY_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "recapDate"));
                template.addElement(MODE_DAILY, parameter);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterDate = Utility.getParameterEmpty(getRequest(), "recapDate");
                if (!"".equals(filterDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&recapDate=" + Utility.getPostParameterEmpty(this, "recapDate"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                    getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                    getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));

                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("2".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_WEEKLY);
                Utility.replaceAll(parameter, WEEKLY_SELECT_START_DATE, Dictionary.getValue(this, "Start date"));
                Utility.replaceAll(parameter, WEEKLY_START_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "startDate"));
                template.addElement(MODE_WEEKLY, parameter);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                if (!"".equals(filterStartDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&startDate=" + Utility.getParameterEmpty(getRequest(), "startDate"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime((Date) dateFormatter.parse(filterStartDate));
//                    for (int i = 0; i < 7; i++) {
//                        String start = dateFormatter.format(cal.getTime());
//                        cal.add(Calendar.DATE, 1);
//                        String end = dateFormatter.format(cal.getTime());

                    String start = dateFormatter.format(cal.getTime());
                    cal.add(Calendar.DATE, 7);
                    String end = dateFormatter.format(cal.getTime());
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                    getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                    getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    getController().setPaginationFilter("&mode=" + mode + "&startDate=" + filterStartDate);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("3".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_MONTHLY);
                Utility.replaceAll(parameter, MONTHLY_SELECT_YEAR, Dictionary.getValue(this, "Year"));
                String selectYear = "<option value=\"\">" + Dictionary.getValue(this, "Select year") + "</option>";
                int year = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = year; i >= 2010; i--) {
                    selectYear += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapYear")) ? " selected" : "") + ">" + i + "</option>";
                }
                Utility.replaceAll(parameter, MONTHLY_SELECT_YEAR_VALUES, selectYear);
                Utility.replaceAll(parameter, MONTHLY_SELECT_MONTH, Dictionary.getValue(this, "Month"));
                String selectMonth = "<option value=\"\">" + Dictionary.getValue(this, "Select month") + "</option>";
                for (int i = 1; i <= 12; i++) {
                    selectMonth += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapMonth")) ? " selected" : "") + ">" + Dictionary.getValue(this, Utility.getLongMonthName(i)) + "</option>";
                }
                Utility.replaceAll(parameter, MONTHLY_SELECT_MONTH_VALUES, selectMonth);
                template.addElement(MODE_MONTHLY, parameter);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                String filterMonth = Utility.getParameterEmpty(getRequest(), "recapMonth");
                if (!"".equals(filterYear) && !"".equals(filterMonth)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&recapYear=" + Utility.getPostParameterEmpty(this, "recapYear") + "&recapMonth=" + Utility.getPostParameterEmpty(this, "recapMonth"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", filterMonth);
                    Utility.replaceAll(date, "dd", "01");
                    String start = date.toString();
                    Calendar cal = Calendar.getInstance();
                    cal.setTime((Date) dateFormatter.parse(start));
                    int max = cal.getActualMaximum(Calendar.DATE);
                    date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", filterMonth);
                    Utility.replaceAll(date, "dd", "" + max);
                    String end = date.toString();
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                    getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                    getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    getController().setPaginationFilter("&mode=" + mode + "&recapYear=" + filterYear + "&filterMonth=" + filterMonth);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("4".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_YEARLY);
                Utility.replaceAll(parameter, YEARLY_SELECT_YEAR, Dictionary.getValue(this, "Year"));
                String selectYear = "<option value=\"\">" + Dictionary.getValue(this, "Select year") + "</option>";
                int year = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = year; i >= 2010; i--) {
                    selectYear += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapYear")) ? " selected" : "") + ">" + i + "</option>";
                }
                Utility.replaceAll(parameter, YEARLY_SELECT_YEAR_VALUES, selectYear);
                template.addElement(MODE_YEARLY, parameter);
                template.addElement(MODE_CUSTOM, null);
                String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                if (!"".equals(filterYear)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&recapYear=" + Utility.getPostParameterEmpty(this, "recapYear"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", "01");
                    Utility.replaceAll(date, "dd", "01");
                    String start = date.toString();
                    date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", "12");
                    Utility.replaceAll(date, "dd", "31");
                    String end = date.toString();
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                    getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                    getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    getController().setPaginationFilter("&mode=" + mode + "&recapYear=" + filterYear);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("5".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                String filterEndDate = Utility.getParameterEmpty(getRequest(), "endDate");
                if (!"".equals(filterStartDate) && "".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp >= :start");
                    getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));

                    complete = true;
                } else if ("".equals(filterStartDate) && !"".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp <= :end");
                    getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));

                    complete = true;
                } else if (!"".equals(filterStartDate) && !"".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&filter=" + Utility.getPostParameterEmpty(this, "filter") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    if (isGranted(actionExport)) {
                        template.addElement(RECAP_EXPORT, export);
                    } else {
                        template.addElement(RECAP_EXPORT, null);
                    }

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    getController().setWhere("t.saleShopTimestamp BETWEEN :start AND :end");
                    getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                    getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));

                    complete = true;
                }
                StringBuffer parameter = template.getEmptyElement(MODE_CUSTOM);
                Utility.replaceAll(parameter, CUSTOM_SELECT_START_DATE, Dictionary.getValue(this, "Start date"));
                Utility.replaceAll(parameter, CUSTOM_START_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "startDate"));
                Utility.replaceAll(parameter, CUSTOM_SELECT_END_DATE, Dictionary.getValue(this, "End date"));
                Utility.replaceAll(parameter, CUSTOM_END_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "endDate"));
                template.addElement(MODE_CUSTOM, parameter);
            }
            template.addValue(BUTTON_SEARCH, Dictionary.getValue(this, "Search"));
            int record = getController().getRecordCount();
            template.addValue(RECORD, Dictionary.getValue(this, "Total all record") + ": " + NumberFormatter.getInstance().format(record));
            if (record > 10000000) {
                template.addValue(WARNING, "return confirm('" + Dictionary.getValue(this, "Records is to big need long time to process, approximately") + " " + NumberFormatter.getInstance().format(record) + " " + Dictionary.getValue(this, "records, are you sure want to continue?") + "');");
            } else {
                template.addValue(WARNING, "");
            }
            if (complete) {
                template.addValue(LIST_DATA, PageList.process(this).toString());
            } else {
                template.addValue(LIST_DATA, "-- " + Dictionary.getValue(this, "Please select parameter") + " --");
            }

            Utility.replaceAll(content, Template.CONTENT_CONTENT, template.process().toString());
            updateContent(content, getMenu(), true);
            return content;
        }
    }
}
